"use strict"  // Use strict JavaScript mode
    
// Pull in the modules we're going to use
var cocos  = require('cocos2d')   // Import the cocos2d module
  , nodes  = cocos.nodes          // Convenient access to 'nodes'
  , events = require('events')    // Import the events module
  , geom    = require('geometry')  // Import the geometry module
  , ccp    = geom.ccp             // Short hand to create points
  ,Texture2D = require('cocos2d/Texture2D').Texture2D
  
// Convenient access to some constructors
var Layer    = nodes.Layer
  , Scene    = nodes.Scene
  , Label    = nodes.Label
  , Director = cocos.Director
  , Animation        = cocos.Animation
  , Animate = cocos.actions.Animate
  , AnimationCache   = cocos.AnimationCache
  , Sequence = cocos.actions.Sequence
  , SpriteFrameCache = cocos.SpriteFrameCache
  , Sprite = cocos.nodes.Sprite

function pause_scene(){
	        pause_scene.superclass.constructor.call(this)
	
	this.isKeyboardEnabled = true
	this.keysDown = new Array()
	this.scheduleUpdate()
	
		// Add a background sprite.
		/*var sprite_bg = new cocos.nodes.Sprite({
		    file: '/resources/fps_0.png',
		    rect: new geom.Rect(0,0, 655,516)
		});
		 
		sprite_bg.anchorPoint = new geom.Point(0, 0);
		sprite_bg.position = new geom.Point(-10,-7);
		this.addChild({child: sprite_bg});
		this.contentSize = sprite_bg.contentSize;
		
		var sprite_fade = new cocos.nodes.Sprite({
		    file: '/resources/pause_fade.png',
		    rect: new geom.Rect(0,0, 640,480)
		});
		 
		sprite_fade.anchorPoint = new geom.Point(0, 0);
		sprite_fade.position = new geom.Point(0,0);
		this.addChild({child: sprite_fade});
		this.contentSize = sprite_fade.contentSize;*/
		

		
		// Menu buttons....................................................................................................................................		
		var winSize = Director.sharedDirector.winSize

		var item1 = new cocos.nodes.MenuItemImage({ 
					normalImage: "/resources/main_menu.png",
                                        selectedImage: "/resources/main_menu.png",
					callback: this.toTitleCallback.bind(this) })
			
		this.contentSize = item1.contentSize
		item1.anchorPoint = new geom.Point(0,0)
		item1.position = ccp(winSize.width / 2, winSize.height / 2 - 100) //new geom.Point(130,160)	
					
		var item2 = new cocos.nodes.MenuItemImage({ 
					normalImage: "/resources/return.png",
                                        selectedImage: "/resources/return.png",
					callback: this.toInGameCallback.bind(this) })
			
		this.contentSize = item2.contentSize
		item2.anchorPoint = new geom.Point(0,0)
		item2.position = ccp(winSize.width / 2, winSize.height / 2 - 200)//new geom.Point(130,100)	
		
		var menu = new cocos.nodes.Menu({ items: [item1,item2] })
		menu.position = geom.ccp(0, 0)
		this.addChild({child: menu})
		//*******************************************************************************************
		
		
		    
		//*******************************************************************************************
		    var frameCache = SpriteFrameCache.sharedSpriteFrameCache
		      , animCache  = AnimationCache.sharedAnimationCache

		    frameCache.addSpriteFrames({ file: '/resources/animations/paused_anim.plist' })


		    // create 'dance' animation
		    var animFrames = []
		      , frame
		     
			frame = frameCache.getSpriteFrame({ name: 'paused_back.png' })
			animFrames.push(frame)
			frame = frameCache.getSpriteFrame({ name: 'paused_noback.png' })
			animFrames.push(frame)

		    var animation = new Animation({ frames: animFrames
						  , delay: 0.2
						  })

		    // Add an animation to the Cache
		    animCache.addAnimation({ animation: animation
					   , name: 'paused_anim'
					   })


		    var normal     = animCache.getAnimation({ name: 'paused_anim' })
		    
		    var animN = new Animate({ animation: normal })

		    var seq = new Sequence({ actions: [animN] })

		    // create an sprite without texture
		    var pausedSprite = new Sprite()

		    var winSize = Director.sharedDirector.winSize

		    pausedSprite.position = ccp(winSize.width / 2, winSize.height / 2)

		    this.addChild({ child: pausedSprite })


		    // run the animation
		pausedSprite.runAction(new cocos.actions.RepeatForever(seq))
		//*******************************************************************************************
}


pause_scene.inherit(cocos.nodes.Layer, {
    _title: 'No title',
    subtitle: null,
    keysDown: null,

    get title () {
        return this._title
    },

    set title (newTitle) {
        this._title = newTitle
    },

    restartCallback: function () {
        var director = cocos.Director.sharedDirector

        var scene = new nodes.Scene()
        var action = restartAction()
        scene.addChild({ child: new action() })

        director.replaceScene(scene)
    },

    backCallback: function () {
        var director = cocos.Director.sharedDirector

        var scene = new nodes.Scene()
        var action = backAction()
        scene.addChild({ child: new action() })

        director.replaceScene(scene)
    },

    nextCallback: function () {
        var director = cocos.Director.sharedDirector

        var scene = new nodes.Scene()
        var action = nextAction()
        scene.addChild({ child: new action() })

        director.replaceScene(scene)
    },
    
    toTitleCallback: function (){
        cocos.Director.sharedDirector.popScene()
	cocos.Director.sharedDirector.popScene()
	cocos.Director.sharedDirector.popScene()
	cocos.Director.sharedDirector.popScene()
    },
    
    toInGameCallback: function (){
        cocos.Director.sharedDirector.popScene()
    },
    
    // Keyboard events
    keyDown: function (evt) {
        var kc = evt.keyCode;
	console.log('down keycode' + kc );
	    
        this.keysDown[kc] = new Boolean(true);
	    
    },
    keyUp: function (evt) {
        var kc = evt.keyCode;
	console.log('up keycode' + kc );
	    
        this.keysDown[kc] = new Boolean(false)
	
    },
    update: function (dt) {
	    
	// Check for 'enter'
	if( this.keysDown[13] == true){
		cocos.Director.sharedDirector.popScene()
	}
	
    },
})

module.exports = pause_scene